from django.shortcuts import render, redirect, HttpResponseRedirect
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from aicourseapp.models import *
from django.http import JsonResponse
from flask import request
from django.forms.models import model_to_dict  
from django.contrib.auth.decorators import login_required
from rest_framework.decorators import api_view
from django.core.paginator import Paginator

# Create your views here.
# 注册新用户
# @api_view(['POST'])
def register_handle(request):
    data = request.POST.dict()
    username = data['username']
    password = data['password']
    if UserInfo.objects.filter(username=username):
        return JsonResponse({"code": "0", "msg": "用户名已存在,请重新输入!"})
    user = UserInfo()
    user.username = username
    user.password = password
    user.save()
    return JsonResponse({"code": "1"})

# 登录
# @api_view(['POST'])
def login_handle(request):
    data = request.POST.dict()
    username = data["username"]
    password = data["password"]
    if UserInfo.objects.filter(username=username, password=password):
        return JsonResponse({'code': "1"})
    else:
        return JsonResponse({'code': "0", "msg": "用户名或者密码错误"})

# 获取轮播图 
def lunbotu_handle(request):
    lunbotus = LunBoTu.objects.all()
    imgarr = []
    for lunbotu in lunbotus:
        img = model_to_dict(lunbotu)
        imgarr.append(img)
    return JsonResponse({"code": "1","ImgArray": imgarr})

# 获取实战推荐数据
def shizhantuijian_handle(request):
    shizhans = ShiZhanTuiJian.objects.all()
    actualarr = []
    for shizhan in shizhans:
        actual = model_to_dict(shizhan)
        actualarr.append(actual)
    return JsonResponse({"code": "1","ActualArr": actualarr})

# 获取新上好课数据
def newclass_handle(request):
    newclasses = NewClass.objects.all()
    newarr = []
    for newclass in newclasses:
        new = model_to_dict(newclass)
        newarr.append(new)
    return JsonResponse({"code": "1","NewArr": newarr})

# 获取新手入门数据
def newentry_handle(request):
    newentrys = NewEntry.objects.all()
    entryarr = []
    for newentry in newentrys:
        new = model_to_dict(newentry)
        entryarr.append(new)
    return JsonResponse({"code": "1","EntryArr": entryarr})

# 获取技能提升数据
def skillup_handle(request):
    skillups = SkillUp.objects.all()
    skillarr = []
    for skillup in skillups:
        new = model_to_dict(skillup)
        skillarr.append(new)
    return JsonResponse({"code": "1","SkillArr": skillarr})

# 获取前沿技术升数据
def cuttingedge_handle(request):
    cuttingedges = CuttingEdge.objects.all()
    edgearr = []
    for cuttingedge in cuttingedges:
        new = model_to_dict(cuttingedge)
        edgearr.append(new)
    return JsonResponse({"code": "1","EdgeArr": edgearr})

def courseindex(request):
    courses = SubClassInfo.objects.all()
    miniprograms = SubClassInfo.objects.filter(sctype__id=1)
    htmls = SubClassInfo.objects.filter(sctype__id=2)
    coursearr = []
    miniprogramarr = []
    htmlarr = []
    for course in courses:
        new = model_to_dict(course,exclude=["img"]) 
        new['img']=str(course.img)
        coursearr.append(new)
    for miniprogram in miniprograms:
        new = model_to_dict(miniprogram,exclude=["img"]) 
        new['img']=str(miniprogram.img)
        miniprogramarr.append(new)
    for html in htmls:
        new = model_to_dict(html,exclude=["img"]) 
        new['img']=str(html.img)
        htmlarr.append(new)
    return JsonResponse({
        "code": "1",
        "coursearr": coursearr,
        "miniprogramarr": miniprogramarr,
        "htmlarr": htmlarr
    })